package com.hanson.controller;

import com.hanson.annotation.CacheData;
import com.hanson.annotation.Log;
import com.hanson.annotation.WebLogIgnore;
import com.hanson.common.api.CommonResult;
import com.hanson.common.api.ResultCode;
import com.hanson.pojo.dto.SystemInfo;
import com.hanson.service.IDataCountService;
import com.hanson.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 * @program: manager
 * @description: 获取系统信息接口
 * @param:
 * @author: Hanson
 * @create: 2020-09-07 18:41
 **/
@Api(tags = "系统信息接口")
@Slf4j
@Controller
@Log
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private IDataCountService dataCountService;

    //获取系统信息信息
    @ApiOperation("获取系统信息信息")
    @PostMapping("/run/infos")
    @WebLogIgnore
    @ResponseBody
    public CommonResult<SystemInfo> getSystemInfos(){
        String host = systemService.getSysHost();
        String system = systemService.getSystemEdition();
        String core = systemService.getCore();
        String jvmMemory = systemService.getJvmMemory();
        String jvmFreeMemory = systemService.getJvmFreeMemory();
        return new CommonResult<>(ResultCode.SUCCESS, new SystemInfo(host, system, core, jvmMemory, jvmFreeMemory));
    }

    //统计信息
    @ApiOperation("首页统计商城数据")
    @GetMapping("/data/count")
    @CacheData(cacheKey = "DATA_COUNT",dataClass = Object.class,expireTime = 30,timeUnit = TimeUnit.MINUTES)
    @WebLogIgnore
    @ResponseBody
    public Object getDataCounts(){
        Object dataCounts = null;
        try {
            dataCounts = dataCountService.getDataCounts();
        } catch (Exception e) {
            log.error("商城信息统计异常",e);
        }
        return dataCounts;
    }
}
